from mootdx.quotes import Quotes
import time
import redis
import pickle
import configparser
import pandas as pd
import logging
import subprocess
log_format = "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"  # 精确到秒
logging.basicConfig(level=logging.DEBUG, format=log_format, datefmt=date_format)


# 初始化配置解析器
config = configparser.ConfigParser()

# 读取配置文件
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
config.read(current_dir+'/config.ini')


# 获取Redis的配置信息
redis_host = config.get('Redis', 'host')
redis_port = config.getint('Redis', 'port')
redis_db = config.getint('Redis', 'db')
redis_password = config.get('Redis', 'password')
r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_db, password=redis_password)


#执行更新服务器的命令
def run_mootdx_bestip():
    command = ['python', '-m', 'mootdx', 'bestip', '-vv']
    subprocess.run(command)

    
while True:
    if r.get("is_trade_time") ==b"NO":
        time.sleep(5*60)
        continue
    logging.info('更新ip地址')
    run_mootdx_bestip()
    client = Quotes.factory(market='std')
    time.sleep(5*60)
